﻿package frEngine.manager
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import baseEngine.utils.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.display3D.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.core.*;
    import frEngine.core.mesh.*;
    import frEngine.effectEditTool.manager.*;
    import frEngine.loaders.resource.info.*;
    import frEngine.shader.*;

    public class Resource3dManager extends EventDispatcher
    {
        private var texure3dIDMap:Dictionary;
        private var tex3dListArray:Vector.<Texture3D>;
        private var loadRList:Vector.<Info3dBase>;
        public var _surfaceMap:Vector.<FrSurface3D>;
        private var _autoToDispose:Boolean = true;
        public var staticResourceDic:Dictionary;
        private var _timer:Timer;
        public static const instance:Resource3dManager = new Resource3dManager;

        public function Resource3dManager()
        {
            this.texure3dIDMap = new Dictionary(false);
            this.tex3dListArray = new Vector.<Texture3D>;
            this.loadRList = new Vector.<Info3dBase>;
            this._surfaceMap = new Vector.<FrSurface3D>;
            this.staticResourceDic = new Dictionary();
            this._timer = new Timer(30 * 60 * 1000);
            this.autoToDispose = false;
            return;
        }// end function

        public function get autoToDispose() : Boolean
        {
            return this._autoToDispose;
        }// end function

        public function set autoToDispose(param1:Boolean) : void
        {
            this._autoToDispose = param1;
            if (param1)
            {
                this._timer.addEventListener(TimerEvent.TIMER, this.checkToDispose);
                this._timer.start();
            }
            else
            {
                this._timer.removeEventListener(TimerEvent.TIMER, this.checkToDispose);
                this._timer.stop();
            }
            return;
        }// end function

        public function stopCheck() : void
        {
            if (this._timer == null)
            {
                return;
            }
            this._timer.removeEventListener(TimerEvent.TIMER, this.checkToDispose);
            this._timer.stop();
            return;
        }// end function

        public function updateFileInfo(param1:String) : void
        {
            var _loc_3:Texture3D = null;
            var _loc_4:Texture3D = null;
            var _loc_2:* = ResourceManager.getInfoByName(param1);
            if (_loc_2)
            {
                _loc_2.dispose();
                _loc_2.clearCacheBytes();
            }
            if (param1.search(/\.jpg|\.png|\.tga|\.dds|\.cmp0|\.cmp1|\.cmp2|\.abc""\.jpg|\.png|\.tga|\.dds|\.cmp0|\.cmp1|\.cmp2|\.abc/i) != -1)
            {
                _loc_3 = Resource3dManager.instance.hasTexture3d(param1, 0);
                _loc_4 = Resource3dManager.instance.hasTexture3d(param1, 1);
                if (_loc_3)
                {
                    _loc_3.request = param1;
                    _loc_3.immediatelyUpload();
                }
                if (_loc_4)
                {
                    _loc_4.request = param1;
                    _loc_4.immediatelyUpload();
                }
            }
            return;
        }// end function

        public function checkToDispose(event:Event = null, param2:Array = null, param3:Array = null) : void
        {
            var _loc_5:Texture3D = null;
            var _loc_6:String = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:ResourceInfo = null;
            var _loc_10:Info3dBase = null;
            var _loc_11:int = 0;
            var _loc_4:* = new Array();
            if (!param2)
            {
                param2 = [];
            }
            if (!param3)
            {
                param3 = [];
            }
            for (_loc_6 in this.texure3dIDMap)
            {
                
                _loc_5 = this.texure3dIDMap[_loc_6];
                if (!this.isStaticResource(_loc_5.request) && param2.indexOf(_loc_5) == -1)
                {
                    _loc_4.push(_loc_5);
                }
            }
            for each (_loc_5 in _loc_4)
            {
                
                this.disposeTexture3dImp(_loc_5, true);
            }
            _loc_4.length = 0;
            _loc_7 = this.loadRList.length;
            _loc_8 = 0;
            while (_loc_8 < _loc_7)
            {
                
                _loc_10 = this.loadRList[_loc_8];
                if (!this.isStaticResource(_loc_10.name) && param3.indexOf(_loc_10.name) == -1)
                {
                    _loc_4.push(_loc_10);
                }
                _loc_8++;
            }
            for each (_loc_9 in _loc_4)
            {
                
                if (_loc_9.isLoaded)
                {
                    _loc_9.dispose();
                    _loc_11 = this.loadRList.indexOf(_loc_9);
                    this.loadRList.splice(_loc_11, 1);
                }
            }
            return;
        }// end function

        private function impDisposeTexture3d(param1:String, param2:Boolean) : void
        {
            var _loc_3:* = this.texure3dIDMap[param1];
            this.checkTexture3d(_loc_3, false);
            if (_loc_3.request is String || _loc_3.targetInfo)
            {
            }
            else
            {
                param2 = true;
                delete this.texure3dIDMap[param1];
            }
            if (param2)
            {
                _loc_3.disposeImp();
            }
            return;
        }// end function

        public function checkTexture3d(param1:Texture3D, param2:Boolean) : void
        {
            var _loc_3:* = this.tex3dListArray.indexOf(param1);
            if (param2)
            {
                if (_loc_3 == -1)
                {
                    this.tex3dListArray.push(param1);
                }
            }
            else if (param1.texture && _loc_3 != -1)
            {
                this.tex3dListArray.splice(_loc_3, 1);
            }
            return;
        }// end function

        public function load(param1:String, param2:Function, param3:int, param4:Function = null) : void
        {
            var _loc_5:* = LoaderManager.instance.load(param1, param2, param3, null, null, param4) as Info3dBase;
            if (!_loc_5)
            {
                return;
            }
            var _loc_6:* = this.loadRList.indexOf(_loc_5);
            if (_loc_6 == -1)
            {
                this.loadRList.push(_loc_5);
            }
            return;
        }// end function

        public function unLoad(param1:String, param2:Function, param3:Function = null) : void
        {
            if (param2 != null || param3 != null)
            {
                LoaderManager.instance.removeResourceEvent(param1, param2, null, param3);
            }
            return;
        }// end function

        public function registerSurface(param1:FrSurface3D) : void
        {
            if (this._surfaceMap.indexOf(param1) == -1)
            {
                this._surfaceMap.push(param1);
            }
            return;
        }// end function

        public function unRegisterSurface(param1:FrSurface3D) : void
        {
            var _loc_2:* = this._surfaceMap.indexOf(param1);
            if (_loc_2 != -1)
            {
                this._surfaceMap.splice(_loc_2, 1);
            }
            return;
        }// end function

        public function hasTexture3d(param1, param2:int) : Texture3D
        {
            var _loc_3:* = Texture3DUtils.getTexureId(param1, param2);
            return this.texure3dIDMap[_loc_3];
        }// end function

        public function getTexture3d(param1, param2:int, param3:int = 3) : Texture3D
        {
            var _loc_4:* = Texture3DUtils.getTexureId(param1, param2);
            var _loc_5:* = this.texure3dIDMap[_loc_4];
            if (!_loc_5)
            {
                _loc_5 = new Texture3D(param1, param2, 0, param3);
                this.texure3dIDMap[_loc_4] = _loc_5;
            }
            else
            {
                _loc_5.loadPriority = param3;
            }
            this.checkTexture3d(_loc_5, true);
            return _loc_5;
        }// end function

        public function disposeTexture3dImp(param1:Texture3D, param2:Boolean = false) : void
        {
            var _loc_3:* = Texture3DUtils.getTexureId(param1.request, param1.mipMode);
            if (this.texure3dIDMap[_loc_3])
            {
                this.impDisposeTexture3d(_loc_3, param2);
            }
            else
            {
                param1.disposeImp();
            }
            return;
        }// end function

        public function checkToParser(param1:Number) : void
        {
            var _loc_5:Info3dBase = null;
            var _loc_2:* = TimeControler.stageTime;
            var _loc_3:* = this.loadRList.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = this.loadRList[_loc_4];
                if (!_loc_5.proceedParsed && _loc_5.isLoaded)
                {
                    _loc_5.proceedParsing();
                    if (getTimer() - _loc_2 >= param1)
                    {
                        return;
                    }
                }
                _loc_4++;
            }
            return;
        }// end function

        public function checkToUploadTexture(param1:Number) : void
        {
            var _loc_7:Texture3D = null;
            var _loc_2:* = TimeControler.stageTime;
            var _loc_3:* = this.tex3dListArray.length;
            var _loc_4:* = Global3D.isOpenGL ? (1) : (2);
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3)
            {
                
                _loc_7 = this.tex3dListArray[_loc_6];
                if (!_loc_7.texture)
                {
                    if (_loc_5 < _loc_4)
                    {
                    }
                    else
                    {
                        return;
                    }
                }
                _loc_6++;
            }
            return;
        }// end function

        public function reUpload() : void
        {
            this.reUploadSurface();
            this.reUploadTexture();
            return;
        }// end function

        public function reUploadTexture() : void
        {
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Texture3D = null;
            var _loc_1:* = TimeControler.stageTime;
            var _loc_2:Array = [];
            var _loc_3:* = Texture3D.allTexutre3DMap;
            for (_loc_4 in _loc_3)
            {
                
                _loc_2.push(_loc_4);
            }
            _loc_5 = _loc_2.length;
            _loc_6 = 0;
            while (_loc_6 < _loc_5)
            {
                
                _loc_7 = _loc_2[_loc_6];
                _loc_6++;
            }
            return;
        }// end function

        private function reUploadSurface() : void
        {
            var _loc_5:FrSurface3D = null;
            var _loc_1:* = TimeControler.stageTime;
            var _loc_2:* = this._surfaceMap.length;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2)
            {
                
                _loc_5 = this._surfaceMap[_loc_4];
                _loc_5.reUpload();
                _loc_4++;
            }
            return;
        }// end function

        public function checkToUploadSurface(param1:Number, param2:Context3D) : void
        {
            var _loc_9:Surface3D = null;
            var _loc_3:* = TimeControler.stageTime;
            var _loc_4:* = this._surfaceMap.length;
            var _loc_5:int = 0;
            var _loc_6:* = Global3D.isOpenGL ? (1) : (2);
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_4)
            {
                
                _loc_9 = this._surfaceMap[_loc_8];
                if (_loc_9.needUpdate)
                {
                    if (_loc_7 < _loc_6 || _loc_9.impUpload)
                    {
                    }
                }
                _loc_8++;
            }
            return;
        }// end function

        public function removeStaticResource(... args) : void
        {
            args = null;
            for each (args in args)
            {
                
                if (args)
                {
                    args = args.replace(/\.effect""\.effect/gi, "");
                    delete this.staticResourceDic[args];
                }
            }
            return;
        }// end function

        public function addStaticResource(... args) : void
        {
            args = null;
            for each (args in args)
            {
                
                if (args)
                {
                    args = args.replace(/\.effect""\.effect/gi, "");
                    this.staticResourceDic[args] = true;
                }
            }
            return;
        }// end function

        public function isStaticResource(param1:String) : Boolean
        {
            return this.staticResourceDic[param1] != null;
        }// end function

        private static function checkResource(param1:Pivot3D, param2:Array, param3:Array) : void
        {
            var _loc_4:MaterialParams = null;
            var _loc_5:Vector.<FsParam> = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Texture3D = null;
            var _loc_9:Vector.<Texture3D> = null;
            var _loc_10:Md5Mesh = null;
            var _loc_11:ParticleMesh = null;
            var _loc_12:NormalMesh3D = null;
            if (param1 is Mesh3D)
            {
                _loc_4 = Mesh3D(param1).material.materialParams;
                _loc_5 = _loc_4.program && _loc_4.program.textures ? (_loc_4.program.textures) : (null);
                if (_loc_5)
                {
                    _loc_6 = _loc_5.length;
                    _loc_7 = 0;
                    while (_loc_7 < _loc_6)
                    {
                        
                        _loc_8 = _loc_5[_loc_7].value;
                        _loc_7++;
                    }
                }
                else
                {
                    _loc_9 = _loc_4.fragmentFilter && _loc_4.fragmentFilter.texture3DList ? (_loc_4.fragmentFilter.texture3DList) : (null);
                    if (_loc_9)
                    {
                        _loc_6 = _loc_9.length;
                        _loc_7 = 0;
                        while (_loc_7 < _loc_6)
                        {
                            
                            _loc_8 = _loc_9[_loc_7];
                            _loc_7++;
                        }
                    }
                }
                if (param1 is Md5Mesh)
                {
                    _loc_10 = Md5Mesh(param1);
                }
                else if (param1 is ParticleMesh)
                {
                    _loc_11 = ParticleMesh(param1);
                }
                else if (param1 is NormalMesh3D)
                {
                    _loc_12 = NormalMesh3D(param1);
                }
            }
            return;
        }// end function

        public static function renderingTexture(param1:Pivot3D, param2:Array, param3:Array) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Obj3dContainer = null;
            var _loc_7:Array = null;
            var _loc_8:Pivot3D = null;
            var _loc_9:Vector.<Pivot3D> = null;
            checkResource(param1, param2, param3);
            if (param1 is Obj3dContainer)
            {
                _loc_6 = Obj3dContainer(param1);
                _loc_7 = _loc_6.getAllObject3d();
                _loc_4 = _loc_7.length;
                _loc_5 = 0;
                while (_loc_5 < _loc_4)
                {
                    
                    _loc_8 = _loc_7[_loc_5];
                    checkResource(_loc_8, param2, param3);
                    _loc_5++;
                }
            }
            else
            {
                _loc_9 = param1.children;
                _loc_4 = _loc_9.length;
                _loc_5 = 0;
                while (_loc_5 < _loc_4)
                {
                    
                    renderingTexture(_loc_9[_loc_5], param2, param3);
                    _loc_5++;
                }
            }
            return;
        }// end function

    }
}
